package com.shiguiwu.springmybatis.algorithm.linkedlist;


/**
 * @description: 双向链表
 * @author: stone
 * @date: Created by 2021/3/3 23:10
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.linkedlist
 */
public class DoubleLinkedList {


    private HeroNode head;

    public DoubleLinkedList() {
        this.head = new HeroNode(0, "水浒传", "");
    }

    public void add(HeroNode node) {
        HeroNode temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = node;
        node.prev = temp;
    }

    /**
     * 添加顺序节点。。。。。
     * @param node
     */
    public void addBySort(HeroNode node) {
        HeroNode temp = head;
        while (temp.next != null ) {
            if (temp.next.no > node.no) {
                //temp的前一个节点的后一个为node
                temp.next.prev = node;
                node.next = temp.next;
                //第一部分结束
                //======================================
                temp.next = node;
                node.prev = temp;
                return;
            }
            temp = temp.next;


        }
        temp.next = node;
        node.prev = temp;
    }

    public void update(HeroNode newNode) {
        HeroNode temp = head.next;
        this.checkNode(temp);
        boolean flag = false;

        while (true) {
            if (temp == null) {
                break;//链表遍历完，未找到
            }
            if (temp.no == newNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }

        if (flag) {
            temp.name = newNode.name;
            temp.nickname = newNode.nickname;
        }
        else {
            System.out.println("未找到修改节点~~~" + newNode);
        }
    }

    public void delete(int no) {
        HeroNode temp = head.next;
        this.checkNode(temp);
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;//链表遍历完，未找到
            }
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;//后移，遍历
        }

        if (flag) {
            temp.prev.next = temp.next;
            //todo 有问题，最后一个节点的时候
            assert temp.next != null;
            temp.next.prev = temp.prev;
        }
        else {
            System.out.println("未找到删除节点~~~" + no);
        }


    }

    /**
     * 打印链表
     */
    public void print() {
        HeroNode temp = head.next;
        this.checkNode(temp);
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }


    }

    public void checkNode(HeroNode temp) {
        if (temp == null) {
            System.out.println("链表为空，不支持操作~~");
            throw new UnsupportedOperationException("链表为空，不支持操作~~");
        }
    }



    //水浒英雄
    public static class HeroNode {
        //链表的顺序
        private int no;

        //名称
        private String name;

        //昵称
        private String nickname;

        //下一个节点
        private HeroNode next;

        //前一个节点
        private HeroNode prev;

        public HeroNode(int no, String name, String nickname) {
            this.no = no;
            this.name = name;
            this.nickname = nickname;
        }

        @Override
        public String toString() {
            return "HeroNode{" +
                    "no=" + no +
                    ", name='" + name + '\'' +
                    ", nickname='" + nickname + '\'' +
                    '}';
        }
    }
}
